package org.pointex.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.pointex.common.controller.BaseController;
import org.pointex.common.model.PageEntity;
import org.pointex.common.model.PagingResult;
import org.pointex.common.util.constant.Constant;
import org.pointex.dao.impl.ConvertIndentDaoImpl;
import org.pointex.dao.impl.ConvertOrderStatusDaoImpl;
import org.pointex.dao.impl.DeliveryInfoDaoImpl;
import org.pointex.dao.impl.ProductDaoImpl;
import org.pointex.domain.ConvertIndent;
import org.pointex.domain.ConvertOrderStatus;
import org.pointex.domain.DeliveryInfo;
import org.pointex.domain.Product;
import org.pointex.domain.Users;
import org.pointex.service.MaxNoService;
import org.pointex.service.UsersService;
import org.pointex.util.AjaxResult;
import org.pointex.util.DateUtil;
import org.pointex.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;


/**
 * 
 * @ClassName: ProductController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:34
 *
 */
@Controller
@RequestMapping("/View/product/")
public class ProductController extends BaseController {
	
	@Autowired
	private ProductDaoImpl pdao;
	
	@Autowired
	private DeliveryInfoDaoImpl deliveryInfoDaoImpl;
	
	@Autowired
	private ConvertIndentDaoImpl convertIndentDaoImpl;
	
	@Autowired
	private ConvertOrderStatusDaoImpl convertOrderStatusDaoImpl;
	
	@Autowired
	private UsersService usersService;
	
	@Autowired
	private MaxNoService maxNoService;

	@RequestMapping("slider")
	public ModelAndView slider() {
		return new ModelAndView();
	}
	
	@RequestMapping(value = "getProductList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getProductList(@Valid int offset,HttpServletResponse response){
		JSONObject jsonObject= new JSONObject();
		try {
			if (offset==0)
				offset=1;
			Users users= getUsersItem(request);
			Map param = new HashMap();
			//筛选会员单位的商品
			param.put("status", 1);
			param.put("delFlag", 0);
			param.put("memberId", users.getMemberId());
			PageEntity pageEntity = new PageEntity(offset, 6, param, "id", "desc");
			PagingResult<Product> prResult = pdao.selectPagination(pageEntity);
			for (Product product : prResult.getResultList()) {
				if(users.getCredits()>=product.getCredits())
					product.setIsBuy(true);
				else
					product.setIsBuy(false);
			}
			jsonObject.put("item", prResult);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally{
			try {
				feedback(response, jsonObject.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@RequestMapping(value = "productExchange", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void productExchange(@Valid String productId,HttpServletResponse response){
		JSONObject jsonObject= new JSONObject();
		AjaxResult result = AjaxResult.ErrorResult();
		try {
			Users users= getUsersItem(request);
			Map param= new HashMap();
			param.put("productId", productId);
			Product product= pdao.selectParam(param).get(0);//获取商品信息
			/*获取用户默认收货地址*/
			Map param2 = new HashMap();
			param2.put("userId", users.getUserId());
			param2.put("delFlag", false);
			param2.put("status", 1);
			
			List<DeliveryInfo> deliveryInfos= deliveryInfoDaoImpl.selectParam(param2);

			if(product.getCredits()>users.getCredits())
				result.setMessage("兑换商品积分不足");//兑换商品积分不足
			else if(deliveryInfos.size()<1)
				result.setMessage("请填写默认收货地址");//请填写默认收货地址
			else{//进行下单
				ConvertIndent convertIndent=new ConvertIndent();
				convertIndent.setCovertId(maxNoService.updateGetNextNo(Constant.MAXNO_CI_NO));
				convertIndent.setUserName(deliveryInfos.get(0).getUserName());
				convertIndent.setUserId(users.getUserId());
				convertIndent.setPhone(deliveryInfos.get(0).getPhone());
				convertIndent.setAddress(deliveryInfos.get(0).getAddress());
				convertIndent.setProductId(productId);
				convertIndent.setSum(1);
				convertIndent.setPlaceTime(DateUtil.formatDate_YYYYMMDDHHMMSS(new Date()));
				convertIndent.setMemberUnit(users.getMemberId());
				convertIndent.setCreateDt(DateUtil.formatDate_YYYYMMDDHHMMSS(new Date()));
				convertIndent.setUpdateDt(DateUtil.formatDate_YYYYMMDDHHMMSS(new Date()));
				convertIndent.setUpdateOper(users.getUserId());
				convertIndent.setStatus(new Byte((byte) 1));
				convertIndent.setDelFlag(new Byte((byte) 0));
				int ciresult=convertIndentDaoImpl.insert(convertIndent);
				
				//添加订单状态
				ConvertOrderStatus convertOrderStatus= new ConvertOrderStatus();
				convertOrderStatus.setOrderId(convertIndent.getCovertId());
				convertOrderStatus.setOrderStatus(new Byte((byte) 1));
				convertOrderStatus.setCreateDt(DateUtil.formatDate_YYYYMMDDHHMMSS(new Date()));
				convertOrderStatus.setUpdateDt(DateUtil.formatDate_YYYYMMDDHHMMSS(new Date()));
				convertOrderStatus.setUpdateOper(users.getUserId());
				convertOrderStatus.setStatus(new Byte((byte) 1));
				convertOrderStatus.setDelFlag(new Byte((byte) 0));
				int coresult=convertOrderStatusDaoImpl.insert(convertOrderStatus);
				if(ciresult+coresult<2)
					result.setMessage("兑换失败!");
				else{
					result=AjaxResult.SuccessResult();//兑换成功
					usersService.updateCredits(users, product.getCredits(), 2,"CB00005","兑换商品");//更新兑换商品所得积分
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setMessage("error！");
		}finally{
			jsonObject.put("result", result);
			try {
				feedback(response, jsonObject.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	@RequestMapping("ware")
	public ModelAndView ware() {
		return new ModelAndView();
	}

}
